What is semver-truncate?
The semver-truncate npm package is designed to truncate semantic versions to a specified level of precision. It can be used to manipulate version strings, allowing developers to easily reduce a full semantic version to just the major, minor, or patch component. This can be particularly useful in scenarios where software compatibility or versioning policies require simplified version strings.
What are semver-truncate's main functionalities?
Truncate to major version
This feature allows you to truncate a semantic version to its major component. It's useful when you need to reference or work with the major version of a dependency or project.
"const semverTruncate = require('semver-truncate');
console.log(semverTruncate('1.2.3', 'major')); // '1'"
Truncate to minor version
This feature enables truncation of a semantic version to include just the major and minor components, omitting the patch level. It can be useful for defining compatibility ranges or when minor version precision is required.
"const semverTruncate = require('semver-truncate');
console.log(semverTruncate('1.2.3', 'minor')); // '1.2'"
Truncate to patch version
This feature allows for the full semantic version string to be used, including the patch level. It's essentially a no-op in this context but can be used for consistency in code that dynamically selects the truncation level.
"const semverTruncate = require('semver-truncate');
console.log(semverTruncate('1.2.3', 'patch')); // '1.2.3'"
Other packages similar to semver-truncate
semver
The semver package is a more comprehensive tool for working with semantic versions. It includes functionality for parsing, comparing, and manipulating semantic versions beyond simple truncation. Compared to semver-truncate, semver offers a broader set of features for detailed version management.
compare-versions
compare-versions is another npm package that allows for comparison of semantic version numbers. While it focuses more on the comparison aspect rather than truncation, it shares the common ground of manipulating version strings for various purposes.
semver-truncate
Truncate a semver version: 1.2.3
→ 1.2.0
Install
$ npm install --save semver-truncate
Usage
const semverTruncate = require('semver-truncate');
semverTruncate('1.2.3-foo', 'patch');
semverTruncate('1.2.3', 'minor');
semverTruncate('1.2.3', 'major');
API
truncateSemver(version, type)
version
Type: string
Semver version.
type
Type: string
Values: patch
minor
major
Version type to truncate to.
License
MIT © Sindre Sorhus